home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Clip Drag and Drop / NotepadClone / NotepadClone.cs next >
Encoding:
Text File  |  2001-01-15  |  2.0 KB  |  60 lines

  1. //-------------------------------------------
  2. // NotepadClone.cs ⌐ 2001 by Charles Petzold
  3. //-------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7.  
  8. class NotepadClone: NotepadCloneWithPrinting
  9. {
  10.      public new static void Main()
  11.      {
  12.                // This needs to be done for drag-and-drop to work. 
  13.  
  14.           System.Threading.Thread.CurrentThread.ApartmentState = 
  15.                                         System.Threading.ApartmentState.STA;
  16.  
  17.           Application.Run(new NotepadClone());
  18.      }
  19.      public NotepadClone()
  20.      {
  21.           strProgName = "NotepadClone";
  22.           MakeCaption();
  23.  
  24.           txtbox.AllowDrop = true;
  25.           txtbox.DragOver += new DragEventHandler(TextBoxOnDragOver);
  26.           txtbox.DragDrop += new DragEventHandler(TextBoxOnDragDrop);
  27.      }
  28.      void TextBoxOnDragOver(object obj, DragEventArgs dea)
  29.      {
  30.           if (dea.Data.GetDataPresent(DataFormats.FileDrop) ||
  31.               dea.Data.GetDataPresent(DataFormats.StringFormat))
  32.           {
  33.                if ((dea.AllowedEffect & DragDropEffects.Move) != 0)
  34.                     dea.Effect = DragDropEffects.Move;
  35.  
  36.                if (((dea.AllowedEffect & DragDropEffects.Copy) != 0) &&
  37.                    ((dea.KeyState & 0x08) != 0))    // Ctrl key
  38.                     dea.Effect = DragDropEffects.Copy;
  39.           }
  40.      }
  41.      void TextBoxOnDragDrop(object obj, DragEventArgs dea)
  42.      {
  43.           if (dea.Data.GetDataPresent(DataFormats.FileDrop))
  44.           {
  45.                if (!OkToTrash())
  46.                     return;
  47.  
  48.                string[] astr = (string[]) 
  49.                                    dea.Data.GetData(DataFormats.FileDrop);
  50.  
  51.                LoadFile(astr[0]);     // In NotepadCloneWithFile.cs
  52.           }
  53.           else if (dea.Data.GetDataPresent(DataFormats.StringFormat))
  54.           {
  55.                txtbox.SelectedText = 
  56.                          (string) dea.Data.GetData(DataFormats.StringFormat);
  57.           }
  58.      }
  59. }
  60.